﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Interactivity;
using System.Windows.Controls.Primitives;
using System.ComponentModel;

namespace HugeFlow.OOB.Behaviors
{
    public class OOBMaximizeWindowBehavior : Behavior<ButtonBase>
    {
        protected override void OnAttached()
        {
            if (OOBHelper.IsRunningOOBWithPermission == false)
            {
                // 블렌드에서 보고 있지 않으면 컨트롤을 감춘다
                if (DesignerProperties.IsInDesignTool == false)
                {
                    AssociatedObject.Visibility = Visibility.Collapsed;
                }
                return;
            }

            AssociatedObject.Click += new RoutedEventHandler(AssociatedObject_Click);
            Application.Current.Host.Content.FullScreenChanged += new EventHandler(Content_FullScreenChanged);
        }

        void Content_FullScreenChanged(object sender, EventArgs e)
        {
            if (Application.Current.Host.Content.IsFullScreen == true)
            {
                AssociatedObject.Visibility = Visibility.Collapsed;
            }
            else
            {
                AssociatedObject.Visibility = Visibility.Visible;
            }
        }

        void AssociatedObject_Click(object sender, RoutedEventArgs e)
        {
            if (sender is ToggleButton)
            {
                ToggleButton toggle = sender as ToggleButton;
                if (toggle.IsChecked == true)
                {
                    Application.Current.MainWindow.WindowState = WindowState.Maximized;
                }
                else
                {
                    Application.Current.MainWindow.WindowState = WindowState.Normal;
                }
            }
            else
            {
                if (Application.Current.MainWindow.WindowState == WindowState.Maximized)
                {
                    Application.Current.MainWindow.WindowState = WindowState.Normal;
                }
                else if (Application.Current.MainWindow.WindowState == WindowState.Normal)
                {
                    Application.Current.MainWindow.WindowState = WindowState.Maximized;
                }
            }
        }

        protected override void OnDetaching()
        {
            base.OnDetaching();

            AssociatedObject.Click -= new RoutedEventHandler(AssociatedObject_Click);
        }
    }
}
